package circleProgressCreator
{
	import flash.display.*;
	import flash.events.*;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.logging.LogEvent;
	import mx.logging.LogEventLevel;
	
	public class CirlceProgress extends MovieClip
	{
		private var _circle_x:Number; //circle X Position
		private var _circle_y:Number; // circle Y Position
		private var _circle_ra:Number // circle Radio
		private var _angle:Number; // Angle tu increment = 1.82 is a good value for preloader at 100 times
		private var _tempX:Number; //temp value for calcuate increment o Draw in X
		private var _tempY:Number; //temp value for calcuate increment o Draw in Y
		private var _shape:Sprite = new Sprite; //Circle Sprite		
		private var _timer:Timer; // timer for testing
		private var _shapeAlpha:Number; // optional for show shape
		private var _shapeColor:uint; // shape color
		private var _lineAlpha:Number; // optional for show line
		private var _lineColor:uint; // line color
		private var _linethikness:int; //line thickness
		private var _linePixelHinting:Boolean=false;
		private var _lineScaleMode:String="normal";
		private var _lineCaps:String="none";
		private var _lineJoints:String="miter";
		private var _miterLimit:Number=3;
		
		
		private var _clockWise:Boolean;
		private var _countDown:Boolean;
		
		private var _incrementValue:Number = -1.82;
		
		private var testOption:Boolean; // option for testing Animation
		private var _value:Number; //value to increment to increse progress
		
		private static var logger:ILogger = Log.getLogger("CirclePreloader");
		
		public function CirlceProgress(_posX:Number=100, _posY:Number=100, _radius:Number=100, _shAlpha:Number=1, _shColor:uint=0x000000, _liAlpha:Number=1, _liColor:uint=0x000000, _liThickness:int=0, _cd:Boolean=false, _cw:Boolean=true, _test:Boolean=false)
		{
			logger.log(LogEventLevel.INFO, "CirclePreloader");
			
			_countDown=_cd;
			_clockWise=_cw;
			
			_circle_x=_posX;
			_circle_y=_posY;
			
			if (_clockWise) {
				_circle_ra=(_radius)/2*-1;
			} else {
				_circle_ra=(_radius)/2;
			}
			_shapeAlpha=_shAlpha;
			_shapeColor=_shColor;
			_lineAlpha=_liAlpha;
			_lineColor=_liColor;
			_linethikness=_liThickness;
			testOption=_test;

			if (_countDown) {
				_value=99;
				updateProgress(_value);
			}
			
			addChild(_shape);
		}

		public function updateProgress(_progress:Number):void {
			var initilized:Boolean;
				_value=_progress;
				if (_value==100) {
					_value=99
				} else if (_value==0) {
					_value=1;
				}
				_shape.graphics.clear();
				_shape.graphics.beginFill(_shapeColor, _shapeAlpha);
				_shape.graphics.lineStyle(_linethikness, _lineColor, _lineAlpha, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
				_shape.graphics.moveTo(_circle_x, _circle_y);
				
				for (var i:Number=0; i>=_angle; i--){
					
					if (_clockWise){
						_tempX=Math.sin(Math.PI/90*i)*_circle_ra + _circle_x;
						_tempY=Math.cos(Math.PI/90*i)*_circle_ra + _circle_y;
						if (initilized) {
							_shape.graphics.lineStyle(_linethikness, _lineColor, _lineAlpha, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
						}else {
							initilized=true;
							_shape.graphics.lineStyle(_linethikness, _lineColor, 0, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
						}
						_shape.graphics.lineTo(_tempX, _tempY);
						_shape.graphics.lineStyle(_linethikness, _lineColor, 0, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
					} else {
						_tempX=Math.cos(Math.PI/90*i)*_circle_ra + _circle_x;
						_tempY=Math.sin(Math.PI/90*i)*_circle_ra + _circle_y;
						if (initilized) {
							_shape.graphics.lineStyle(_linethikness, _lineColor, _lineAlpha, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
						}else {
							initilized=true;
							_shape.graphics.lineStyle(_linethikness, _lineColor, 0, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
						}
						_shape.graphics.lineTo(_tempX, _tempY);
						_shape.graphics.lineStyle(_linethikness, _lineColor, 0, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
					}
					
				}
				
				if (_lineAlpha == 0) {
					_shape.graphics.lineStyle(_linethikness, _lineColor, 0, _linePixelHinting, _lineScaleMode, _lineCaps, _lineJoints, _miterLimit);
					_shape.graphics.endFill();
				} else {
					_shape.graphics.endFill();
				}
				_angle = Math.round(_value * (_incrementValue));
		}
		
		public function set shapeColor(value:uint):void {
			_shapeColor=value;
		}
		
		public function get shapeColor():uint {
			return _shapeColor;
		}	
		
		public function set shapeAlpha(value:Number):void {
			_shapeAlpha=value;
		}
		
		public function get shapeAlpha():Number {
			return _shapeAlpha;
		}
		
		public function set lineColor(value:uint):void {
			_lineColor=value;
		}
		
		public function get lineColor():uint {
			return _lineColor;
		}
		
		public function set lineAlpha(value:Number):void {
			_lineAlpha=value;
		}
		
		public function get lineAlpha():Number {
			return _lineAlpha;
		}
		
		public function set progress(value:Number):void {
			_value:value;
			updateProgress(_value);
		}

		public function get linePixelHinting():Boolean
		{
			return _linePixelHinting;
		}

		public function set linePixelHinting(value:Boolean):void
		{
			_linePixelHinting = value;
		}

		public function get lineCaps():String
		{
			return _lineCaps;
		}

		public function set lineCaps(value:String):void
		{
			_lineCaps = value;
		}

		public function get lineJoints():String
		{
			return _lineJoints;
		}

		public function set lineJoints(value:String):void
		{
			_lineJoints = value;
		}

		public function get lineScaleMode():String
		{
			return _lineScaleMode;
		}

		public function set lineScaleMode(value:String):void
		{
			_lineScaleMode = value;
		}

		
	}
}